/Project |-Database/ |-Binaries/ |-Figures/ |-hawaii |_ beakerBanter_hawaii.Rmd |_ hawaii_banter_data.rds |_ hawaii_banter_data_ici.rds |_ hawaii_banter_model_t5e3s10_t1e4s4.rds |_ hawaii_banter_model_ici_t5e3s5_t1e4s4.rds

PAMpal Data Processing

Start by loading the required packages

library("easypackages")
libraries("PAMpal", "banter", "rfPermute", "kableExtra", "magick", "magrittr", "here")
here()
## [1] "C:/Users/shannon.rankin.NMFS/Documents/GitHub/BANTER_BeakedWhales"
  1. Set up our PPS (PAMPal Settings Object) for the Hawaii dataset
pps <- PAMpalSettings(db='Database/', 
                      binaries = 'Binaries/',
                      sr_hz='auto', 
                      winLen_sec=.0025, 
                      filterfrom_khz=10, 
                      filterto_khz=NULL)
  1. Process data and save to file to eliminate repeated processing.

If this is the initial processing, ensure you have set ‘freshRun = TRUE’ at top of this document to process and save data. This will take some time to run.

data <- processPgDetections(pps, mode='db', id='hawaii_bw')
saveRDS(data, 'hawaii_study.rds')
# Double check warning messages
print(getWarnings(data)$message)

If you have already run the processing code, ensure you have set ‘freshRun = FALSE’ at top of this document to read in the existing .rds file for downstream processing.

  1. Assign species identity according to original PAMguard labels, then relabel for consistency across projects.
data <- setSpecies(data, 'pamguard')
reSpecies <- readRDS('species.rds')
print(reSpecies)
data <- setSpecies(data, 'reassign', value=reSpecies)
  1. Filter out unwanted data: (1) subset and rename species for BANTER model and (2) filter to retain only Channel 1.
goodSpecies<- c("ZC", "MD", "BW", "BWC", "IP", "BW", "possBW")
data <- filter(data, species %in% goodSpecies)
data <- setSpecies(data, method='reassign',
                   value=data.frame(old=c('ZC', 'MD', 'BWC', 'IP'), new=c("Cuviers", "Blainsvilles", "CrossSeamount", "Longmans")))
data_ch1only <- filter(data, Channel == '1')
  1. Calculate Inter-Click Interval (ICI).
data_ch1only <- calculateICI(data_ch1only, time='peakTime')
  1. Export data for BANTER (and drop species codes that will not be used for training). We will create two datasets: one with ICI and one without ICI, and save these for import into BANTER.
banter_data <- export_banter(data_ch1only, dropSpecies = c('BW', 'possBW'), 
                         dropVars = c('All_ici'), training=TRUE)
saveRDS(banter_data, file='hawaii_banter_data.rds')

banter_data_ici <- export_banter(data_ch1only, dropSpecies = c("BW", "possBW"), training=TRUE)
saveRDS(banter_data_ici, file='hawaii_banter_data_ici.rds')

#save update of Acoustic Study
saveRDS(data_ch1only, 'hawaii_study.rds')

Build a BANTER Classification Model

EC (only) Model

Initialize, Run & Evaluate Detector Model (stage 1).

banter_model_ec <- initBanterModel(banter_data$events)
banter_model_ec <- addBanterDetector(banter_model_ec, banter_data$detectors, ntree=5e3, sampsize=10, importance = TRUE)

plotDetectorTrace(banter_model_ec, detector = paste0('Click_Detector_', 0:3))
plotDetectorTrace(banter_model_ec, detector = paste0('Click_Detector_', 4:6))
summary(banter_model_ec)

Run BANTER Event Model (stage 2)

banter_model_ec <- runBanterModel(banter_model_ec, ntree=1e4, sampsize=4)
summary(banter_model_ec)

Once a stable model is identified, save model with tree/sampsize info in the filename.

saveRDS(banter_model_ec, 'hawaii_banter_model_ec_t5e3s10_t1e4s4.rds')

ICI Model

Initialize, Run & Evaluate Detector Model (stage 1)

banter_model_ici <- initBanterModel(banter_data_ici$events)
banter_model_ici <- addBanterDetector(banter_model_ici, banter_data_ici$detectors, ntree=5e3, sampsize=5, importance = TRUE)

plotDetectorTrace(banter_model_ici, detector = paste0('Click_Detector_', 0:3))
plotDetectorTrace(banter_model_ici, detector = paste0('Click_Detector_', 4:6))
summary(banter_model_ici)

Run BANTER Event Model (stage 2)

banter_model_ici <- runBanterModel(banter_model_ici, ntree=1e4, sampsize=4)
summary(banter_model_ici)

Once a stable model is identified, save model with tree/sampsize info in the filename.

saveRDS(banter_model_ici, 'hawaii_banter_model_ici_t5e3s5_t1e4s4.rds')

BANTER Analytics

There are a number of visualizations/data products that allow us to visualize our BANTER classifier; most use the rfPermute package (see BANTER Guidelines for more information)

First, identify the model you would like to examine (comment out the model you do not want to examine).

model_ec <- banter_model_ec
modelname_ec <- "banter_model_ec"

model_ici <- banter_model_ici
modelname_ici <- "banter_model_ici"

Extract the Random Forest model object from our BANTER model for analysis.

banter_model_ec_RF <- getBanterModel(model_ec)
banter_model_ici_RF <- getBanterModel(model_ici)

Class Priors (Expected Error Rate)

hawaii_ec_priors <- classPriors(banter_model_ec_RF, NULL)[,1]
hawaii_ici_priors <- classPriors(banter_model_ici_RF, NULL)[,1]

Confusion Matrix

hawaii_ec_confuseMatrix <- rfPermute::confusionMatrix(banter_model_ec_RF)
hawaii_ec_confuseMatrix <- cbind(hawaii_ec_confuseMatrix, priors = hawaii_ec_priors)
hawaii_ec_confuseMatrix <- kable(hawaii_ec_confuseMatrix, align = "c", digits = c(0,0,0,0,2,2,2))%>%
  kable_classic()%>%
  column_spec(5, border_right = TRUE)%>%
  row_spec(0, bold = TRUE)%>%
  row_spec(5,hline_after = TRUE)%>%
  row_spec(5, bold = TRUE)%>%
  save_kable('../manuscript/manuscript_files/hawaii_ec_confuseMatrix.png', zoom = 9)

hawaii_ici_confuseMatrix <- rfPermute::confusionMatrix(banter_model_ici_RF)
hawaii_ici_confuseMatrix <- cbind(hawaii_ici_confuseMatrix, priors = hawaii_ici_priors)
hawaii_ici_confuseMatrix <- kable(hawaii_ici_confuseMatrix, align = "c", digits = c(0,0,0,0,2,2,2))%>%
  kable_classic()%>%
  column_spec(5, border_right = TRUE)%>%
  row_spec(0, bold = TRUE)%>%
  row_spec(5,hline_after = TRUE)%>%
  row_spec(5, bold = TRUE)%>%
  save_kable('../manuscript/manuscript_files/hawaii_ici_confuseMatrix.png', zoom = 9)
BANTER Model Hawaii EC Confusion Matrix
BANTER Model Hawaii EC Confusion Matrix
BANTER Model Hawaii ICI Confusion Matrix
BANTER Model Hawaii ICI Confusion Matrix

Proximity Plot

png(('../manuscript/manuscript_files/hawaii_ec_proximity.png'), width = 20, height = 20, units = 'cm',  res = 300)
plotProximity(banter_model_ec_RF)
dev.off()
ec_hawaii_proximityPlot <- plotProximity(banter_model_ec_RF)

png(('../manuscript/manuscript_files/hawaii_ici_proximity.png'), width = 20, height = 20, units = 'cm',  res = 300)
ici_hawaii_proximityPlot <- plotProximity(banter_model_ici_RF)
dev.off()
ici_hawaii_proximityPlot <- plotProximity(banter_model_ici_RF)

Importance Heatmap

png(('../manuscript/manuscript_files/hawaii_ec_importance.png'), width = 30, height = 25, units = 'cm',  res = 300)
plotImportance(banter_model_ec_RF, plot.type="heatmap", n=10)
dev.off()
ec_hawaii_importance <- plotImportance(banter_model_ec_RF, plot.type="heatmap", n=10)

png(('../manuscript/manuscript_files/hawaii_ici_importance.png'), width = 30, height = 25, units = 'cm',  res = 300)
plotImportance(banter_model_ici_RF, plot.type="heatmap", n=10)
dev.off()
ici_hawaii_importance <- plotImportance(banter_model_ici_RF, plot.type="heatmap", n=10)

PlotVotes

png(('../manuscript/manuscript_files/hawaii_ec_votes.png'), width = 20, height = 20, units = 'cm',  res = 300)
plotVotes(banter_model_ec_RF)
dev.off()
hawaii_votes <- plotVotes(banter_model_ec_RF)

png(('../manuscript/manuscript_files/hawaii_ici_votes.png'), width = 20, height = 20, units = 'cm',  res = 300)
plotVotes(banter_model_ici_RF)
dev.off()
ici_hawaii_votes <- plotVotes(banter_model_ici_RF)

Plot Predicted Probabilities

plotPredictedProbs(banter_model_ec_RF, bins=30, plot=TRUE)

plotPredictedProbs(banter_model_ici_RF, bins=30, plot=TRUE)

Create Figure for Publication

confuse <- magick::image_read(here('manuscript', 'manuscript_files', 'hawaii_ec_confuseMatrix.png'))%>%
  image_border(color="#ffffff", geometry = "50x130")%>%
  image_annotate("a) Confusion Matrix", size=300, color = "black")
vote <- magick::image_read(here('manuscript', 'manuscript_files', 'hawaii_ec_votes.png'))%>%
  image_border(color="#ffffff", geometry = "270x130")%>%
  image_annotate("d) Vote Plot", size=300, color = "black")
prox <- magick::image_read(here('manuscript', 'manuscript_files', 'hawaii_ec_proximity.png'))%>%
  image_border(color="#ffffff", geometry = "270x130")%>%
  image_annotate("b) Proximity Plot", size=300, color = "black")
heat <- magick::image_read(here('manuscript', 'manuscript_files', 'hawaii_ec_importance.png'))%>%
  image_border(color="#ffffff", geometry = "270x130")%>%
  image_scale("3300")%>%
  image_annotate("d) Importance Heat Map", size=300, color = "black")
  
hawaii_ec_Figure <-image_append(c(prox, heat, vote))
hawaii_ec_Figure<- image_append(c(confuse, hawaii_ec_Figure), stack=TRUE)
image_write(hawaii_ec_Figure, path = here('manuscript', 'manuscript_files','hawaii_ec_Figure.png'), format ='png')
print(hawaii_ec_Figure, info=FALSE)

confuse <- magick::image_read(here('manuscript', 'manuscript_files', 'hawaii_ici_confuseMatrix.png'))%>%
  image_border(color="#ffffff", geometry = "50x130")%>%
  image_annotate("a) Confusion Matrix", size=300, color = "black")
vote <- magick::image_read(here('manuscript', 'manuscript_files', 'hawaii_ici_votes.png'))%>%
  image_border(color="#ffffff", geometry = "270x130")%>%
  image_annotate("d) Vote Plot", size=300, color = "black")
prox <- magick::image_read(here('manuscript', 'manuscript_files', 'hawaii_ici_proximity.png'))%>%
  image_border(color="#ffffff", geometry = "270x130")%>%
  image_annotate("b) Proximity Plot", size=300, color = "black")
heat <- magick::image_read(here('manuscript', 'manuscript_files', 'hawaii_ici_importance.png'))%>%
  image_border(color="#ffffff", geometry = "270x130")%>%
  image_scale("3300")%>%
  image_annotate("d) Importance Heat Map", size=300, color = "black")
  
hawaii_ici_Figure <-image_append(c(prox, heat, vote))
hawaii_ici_Figure<- image_append(c(confuse, hawaii_ici_Figure), stack=TRUE)
image_write(hawaii_ici_Figure, path = here('manuscript', 'manuscript_files','hawaii_ici_Figure.png'), format ='png')
print(hawaii_ici_Figure, info=FALSE)